var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),common_Js = require("../../../component/common.js"),
formatTime = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:false,
    propertyName:'',
    estateName:'',
    commentContent:'',
    submitState:false,
    leaderName:'',
    phoneNum:'',
    keyword:'',
    checkboxXieyi:false,
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    signImg:'',
    ag_id:0,
    cashMoney:20,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    console.log('isLogin',isLogin)
    
    if (options!=undefined && options.q) {
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q', q)
      options = common_Js.getQueryVariable(q);
    }

    if(options.ag_id){
      that.data.ag_id = parseInt(options.ag_id);
    }
    console.log('that.data.ag_id',that.data.ag_id)
    
    if(isLogin == 1){
      var user_id = app.getStorageSync("user_id");
      if(user_id > 0){
        // app.getUserInfoById(user_id)
      }else{
        wx.navigateTo({
          url: '/pages/login/index?ag_id=' + that.data.ag_id,
        })
      }
    }else{
      wx.navigateTo({
        url: '/pages/login/index?ag_id='+that.data.ag_id,
      })
    }
    this.onHttpInit();
  },
  onShow(options){
    console.log('onShow options',options)
    console.log('onShow this.data.signImg',this.data.signImg)
  },
  /**
   * 获取商家邀请注册的 现金奖励金额
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/mer/cash/role/detail', {
    }, function (res) {
      console.log("获取商家邀请注册的", res)
      console.log('res',res)
      if (res.data.code == 0) {
        if(res.data.data.role.maxMoney > 0){
          console.log('res.data.data.role.maxMoney',res.data.data.role.maxMoney)
          that.data.cashMoney = parseInt(res.data.data.role.maxMoney);
          console.log('cashMoney',that.data.cashMoney)
        }
      }
    }, function () {
    })
  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  propertyNameInput: function (t) {
    this.data.propertyName = t.detail || "";
  },
  estateNameInput: function (t) {
    this.data.estateName = t.detail || "";
  },
  phoneNumInput(e){
    this.data.phoneNum = e.detail || "";
  },
  leaderNameInput(e){
    this.data.leaderName = e.detail || "";
  },
  submitOp:function(e){
    var that = this;
    if(that.data.propertyName == '' || that.data.propertyName == undefined){
      Toast("请输入物业公司名称");
      return ;
    }
    if(that.data.estateName == '' || that.data.estateName == undefined){
      Toast("请输入小区名称");
      return ;
    }

    if(that.data.leaderName == '' || that.data.leaderName == undefined){
      Toast("请输入联系人姓名");
      return ;
    }

    if(that.data.phoneNum == '' || that.data.phoneNum == undefined){
      Toast("请输入您的手机号方便与您联系");
      return ;
    }

    that.setData({ submitting : true})

    fetchApi.httpPost('/property/add/register', {
      property_name: that.data.propertyName,
      estate_name: that.data.estateName,
      leader_name: that.data.leaderName,
      leader_phone: that.data.phoneNum,
    }, function (res) {
      console.log('res', res)
      if (res.data.code == 0) {
        var mer_id = res.data.data;
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/home/home',
          })
        },1000)
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },  
})